<?php

namespace App\Kuafu\CustomerCoupon\Jobs;

use App\Kuafu\CustomerCoupon\Model\SendCustomerCouponFailLog;
use App\Kuafu\CustomerCoupon\Service\CustomerCouponService;
use App\Library\FeiShu\SendFs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class SendCouponPolling implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    //任务名称-绑定手机号送券
    const  QUEUE_NAME = '{RMS-BAND-MOBILE-SEND-COUPON}';

    private $tries = 40;
    private $userId;
    private $activityId;
    private $activityType;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userId, $activityId, $activityType)
    {
        $this->userId       = $userId;
        $this->activityId   = $activityId;
        $this->activityType = $activityType;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //日志
        Log::info('绑定手机号发放优惠券进入队列：', ['userId' => $this->userId, 'activityId' => $this->activityId]);
        //给用户添加优惠券
        // 每次间隔5s，最大延迟≈3分钟
        if ($this->attempts() >= $this->tries) {
            Log::info("绑定手机号发放优惠券失败:用户id：{$this->userId}----活动id：{$this->activityId}");
            //todo 飞书报错
            return;
        }
        CustomerCouponService::sendCoupon($this->userId, $this->activityId, $this->activityType);
    }
}
